using System;
using System.Diagnostics;

namespace GraphMyCode.Client.Scalling
{
	public class AbsoluteScaleEngine : ScaleEngine
	{
		private const double WidthCoef = 100;
		private const double ThickCoef = 1;
		private readonly Func<double,double> nodeFunction;
		private readonly Func<double, double> edgeFunction;

		private const double MinThick = 3;
		private const double MaxThick = 10;
		private const double MinFontSize = 9;
		private const double MaxFontSize = 14;

		private const double MinWidth = 20;
		private const double MaxWidth = 45;

		public AbsoluteScaleEngine()
		{
			nodeFunction = d => d;
			edgeFunction = d => d;
		}

		public override double GetActualWidth(double weight)
		{
			Debug.Assert(weight >= 0);

			var width = Math.Sqrt(nodeFunction(weight) * WidthCoef);

			if (width < MinWidth)
				return MinWidth;
			if (width > MaxWidth)
				return MaxWidth;

			return width;
		}

		public override double GetActualThick(double thick)
		{
			Debug.Assert(thick >= 0);

			var actualThick = edgeFunction(thick) * ThickCoef;

			if (actualThick < MinThick)
				return MinThick;
			if (actualThick > MaxThick)
				return MaxThick;

			return actualThick;
		}

		public override double GetFontSize(double weight)
		{
			var font = Math.Sqrt(nodeFunction(weight) * WidthCoef / 2);

			if (font < MinFontSize)
				return MinFontSize;
			if (font > MaxFontSize)
				return MaxFontSize;
			return font;
		}
	}
}